package com.System.LearnSystemTeacher;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.System.LearnSystem.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

class token extends Course   //用于临时储存客户端发来的请求信息
{
	private String token;
	private String role;
	public token() {
		
	}
	
	public String gettoken() {
		return token;
	}
	public void settoken(String token) {
		this.token = token;
	}
	public String getrole() {
		return role;
	}
	public void setrole(String role){
		this.role = role;
	}
}



@WebServlet("/tea/addCourse")
public class addCourse extends HttpServlet{
	
	public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException
	{
		StringBuilder json = new StringBuilder();//读取json请求
        BufferedReader reader = req.getReader();
        String line;
        while ((line = reader.readLine()) != null) {
            json.append(line);
        }
        String jsonStr = json.toString();     
        ObjectMapper objectMapper = new ObjectMapper();
        token tk = objectMapper.readValue(jsonStr, token.class);
        
        objectMapper.registerModule(new JavaTimeModule());//注册驱动保证序列化
        res.setContentType("application/json;charset=UTF-8");//设置响应格式
        PrintWriter out = res.getWriter();
        
        if(tk.gettoken()!=null) 
        {
        	if(Token.checkToken(tk.gettoken())&&"TEACHER".equals(tk.getrole()))//验证令牌和身份
        	{
        	String sql = "INSERT INTO t_course (course_no,course_name,teacher_id,major_id,grade,credit,status,course_type,is_public) VALUES (?,?,?,?,?,?,?,?,?);";
        	//                   从头到尾分别是   课程编号   课程名称   授课教师id 适用专业id 年级  学分   课程状态  课程性质  是否公开（默认不公开）
        	try (Connection conn = sqlConnection.getConnection();PreparedStatement pstmt = conn.prepareStatement(sql)) 
        	{
        		pstmt.setString(1, tk.getCourseNo());
        		pstmt.setString(2, tk.getCourseName());
        		pstmt.setLong(3, tk.getTeacherId());
        		pstmt.setLong(4,tk.getMajorId());
        		pstmt.setInt(5, tk.getGrade());
        		pstmt.setBigDecimal(6,tk.getCredit());
        		pstmt.setInt(7,tk.getStatus());
        		pstmt.setInt(8,tk.getCourseType());
        		pstmt.setInt(9,tk.getIsPublic());
        		int Line = pstmt.executeUpdate();
        		System.out.println("受影响的行数为："+Line);
        		Result<Object> result = Result.success();
            	out.println(objectMapper.writeValueAsString(result));
        	} catch (SQLException e) {
        		  System.err.println("SQL 错误码: " + e.getErrorCode());
        		  System.err.println("SQL 错误消息: " + e.getMessage());
        		    e.printStackTrace();
			}
        	}
        	else
        	{
        		Result<Object> result = Result.error(401,"非授权访问");
        		out.println(objectMapper.writeValueAsString(result));
        	}
        }
        else
        {
        	Result<Object> result = Result.error(401,"非授权访问");
        	out.println(objectMapper.writeValueAsString(result));
        }
       
        
	}
       
    
}
	
